import { getBrandList, postDeleteBrand } from '../../../../api/index'
import { formatTimeTwo } from '../../../../utils/util'

Page({
  data: {
    list: [],
    pageSize: 15,
    currentPage: 1,
    keyword: '',
    reviewStatus: 0,
    reviewStatusStr: '综合',
    isEmpty: false,
    showFilter: false,
    filterColumns: [
      { label: '综合', value: 0 },
      { label: '待审核', value: 1 },
      { label: '审核通过', value: 2 },
      { label: '审核不通过', value: 3 }
    ]
  },
  seeDetail ({ currentTarget: { dataset: { item } } }) {
    wx.$nav.navigateTo(`/supplier/pages/brand/detail/index?id=${item.supplierBrandId}`)
  },
  search ({ detail }) {
    this.setData({ keyword: detail })
    this.getList()
  },
  selectFilter () {
    this.setData({ showFilter: true })
  },
  closeFilterPop () {
    this.setData({ showFilter: false })
  },
  changeFilter ({ detail }) {
    this.setData({ reviewStatus: detail.value, reviewStatusStr: detail.label, showFilter: false })
    this.getList()
  },
  edit ({ currentTarget: { dataset: { item } } }) {
    wx.$nav.navigateTo(`/supplier/pages/brand/edit/index?id=${item.supplierBrandId}`)
  },
  async deleteBrand ({ currentTarget: { dataset: { item } } }) {
    try {
      const { confirm } = await wx.$pro.showModal({ title: '删除品牌', content: '确定要删除该品牌吗？' })
      if (confirm) {
        let swiper = this.selectComponent(`#swiper-cell-${item.supplierBrandId}`)
        await postDeleteBrand({ supplierBrandId: item.supplierBrandId })
        wx.showToast({ title: '删除成功！', icon: 'none' })
        swiper.close()
        this.getList()
      }
    } catch (err) {
      console.log(err)
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async loadMore () {
    try {
      if (this.loading || this.alloaded) return
      this.loading = true
      let query = {
        currentPage: this.data.currentPage,
        pageLines: this.data.pageSize
      }
      if (this.data.keyword) query.keyWord = this.data.keyword
      if (this.data.reviewStatus) query.reviewStatus = this.data.reviewStatus
      const { ResultSet } = await getBrandList({ ...query })
      ResultSet.userData.brandLists.forEach(item => {
        item.createDate = formatTimeTwo(item.createDate, 'Y-M-D')
      })
      this.alloaded = !ResultSet.userData.brandLists.length
      this.data.isEmpty = !ResultSet.userData.brandLists.length
      this.setData({ isEmpty: this.data.isEmpty })
      if (ResultSet.userData.brandLists.length) {
        this.data.currentPage = this.data.currentPage + 1
        this.data.list.push(...ResultSet.userData.brandLists)
        this.setData({
          list: this.data.list,
          currentPage: this.data.currentPage
        })
      }
      this.loading = false
    } catch (err) {
      console.log(err)
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  async getList () {
    try {
      let query = {
        currentPage: 1,
        pageLines: this.data.pageSize
      }
      if (this.data.keyword) query.keyWord = this.data.keyword
      if (this.data.reviewStatus) query.reviewStatus = this.data.reviewStatus
      const { ResultSet } = await getBrandList({ ...query })
      ResultSet.userData.brandLists.forEach(item => {
        item.createDate = formatTimeTwo(item.createDate, 'Y-M-D')
      })
      this.data.isEmpty = ResultSet.userData.brandLists.length && ResultSet.userData.brandLists.length < this.data.pageSize
        if (ResultSet.userData.brandLists.length) this.data.currentPage = 2
        this.setData({
          list: ResultSet.userData.brandLists,
          currentPage: this.data.currentPage,
          isEmpty: this.data.isEmpty
        })
    } catch (err) {
      console.log(err)
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  onLoad () {
    this.getList()
  }
})